#HTML <template>
The <template> HTML element serves as a mechanism for holding HTML fragments, which can either be used later via JavaScript or generated immediately into shadow DOM.
#Attributes
-
shadowrootmode
: Creates a shadow root for the parent element. It is a declarative version of theElement.attachShadow()
method and accepts the same enumerated values.open
: Exposes the internal shadow root DOM for JavaScript (recommended for most use cases).closed
: Hides the internal shadow root DOM from JavaScript.
Note: The HTML parser creates a
ShadowRoot
object in the DOM for the first<template>
in a node with this attribute set to an allowed value. If the attribute is not set, or not set to an allowed value — or if aShadowRoot
has already been declaratively created in the same parent — then anHTMLTemplateElement
is constructed. AHTMLTemplateElement
cannot subsequently be changed into a shadow root after parsing, for example, by settingHTMLTemplateElement.shadowRootMode
.Note: You may find the non-standard
shadowroot
attribute in older tutorials and examples that used to be supported in Chrome 90-110. This attribute has since been removed and replaced by the standardshadowrootmode
attribute. -
shadowrootclonable
: Sets the value of theclonable
property of aShadowRoot
created using this element totrue
. If set, a clone of the shadow host (the parent element of this<template>
) created withNode.cloneNode()
orDocument.importNode()
will include a shadow root in the copy. -
shadowrootdelegatesfocus
: Sets the value of thedelegatesFocus
property of aShadowRoot
created using this element totrue
. If this is set and a non-focusable element in the shadow tree is selected, then focus is delegated to the first focusable element in the tree. The value defaults tofalse
. -
shadowrootserializable
Experimental : Sets the value of theserializable
property of aShadowRoot
created using this element totrue
. If set, the shadow root may be serialized by calling theElement.getHTML()
orShadowRoot.getHTML()
methods with theoptions.serializableShadowRoots
parameter settrue
. The value defaults tofalse
.